##########################################
## Copyright 2012-2013 Ceruti Francesco
##
##
## This file is part of LiSP (Linux Show Player).
##
## LiSP (Linux Show Player) is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LiSP (Linux Show Player) is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with LiSP (Linux Show Player).  If not, see <http://www.gnu.org/licenses/>.
##########################################


def seek(line):
    time = float(line[5:])
    return lambda m: m.seek(time * 1000)


def stop(line):
    return lambda m: m.stop()


def pause(line):
    return lambda m: m.pause()


def volume(line):
    volume = float(line[7:])
    return lambda m: m.dPipe['volume'].set_property('volume', volume)


def speed(line):
    speed = float(line[6:])
    return lambda m: m.dPipe['pitch'].set_property('tempo', speed)

SYNTAX = {
          'seek': seek,
          'stop': stop,
          'pause': pause,
          'volume': volume,
          'speed': speed
          }


def parse(program, media):
    program = program.split(';')
    times = []
    events = []
    for p in program:
        p = p.split(': ')
        for s in SYNTAX:
            if(p[1].startswith(s)):
                time = int(p[0]) + 0.001
                events.append(SYNTAX[s](p[1]))
                times.append(time * 1000)
                break
    return tuple(list(t) for t in zip(*sorted(zip(times, events))))
